<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no" />
	<title>首页</title>
	<link rel="stylesheet" type="text/css" href="../c/aui.css" />
	<link rel="stylesheet" type="text/css" href="../c/animate.css" />
	<style>
		body {
			background-color: transparent;
			/*background-color: #222;*/
		}
		.video_loading {
			position: fixed;
			top: 0rem;
			left: 0rem;
			width: 80px;
			border-radius: 0.5rem;
			overflow: hidden;
			height: 110px;
		}
		.video_loading p {
			position: absolute;
			top: 0;
			height: 110px;
			/*line-height: 110px;*/
			text-align: center;
			z-index: 3;
			color: #fff;
			width: 100%;
			font-size: 0.75rem;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.video_loading div {
			position: absolute;
			top: 0;
			height: 110px;
			z-index: 2;
			width: 100%;
			background: #000;
			opacity: 0.2;
			line-height: 110px;
		}
		.video_loading img {
			top: 0;
			left: 0;
			width: 100%;
			height: 110px;
			position: absolute;
			z-index: 1;
		}
		#jindi span{

		}
	</style>
</head>

<body>
	<div class="video_loading animated slideInLeft">
		<img src="../i/loading_300.png" />
		<div></div>
		<p id="jindi">0%</p>
	</div>
</body>
<script type="text/javascript" src="../j/api.js"></script>
<script type="text/javascript">
	var video_content,videoUrl,imgurl='';
	apiready = function() {
		_setPrefs('index_video_loading', '5Y2X5piM6YCf57+U572R57uc56eR5oqA5pyJ6ZmQ5YWs5Y+4IA0KUVHmioDmnK/vvJoxNDI0NDQ1NjA4ICAg55S16K+dL+W+ruS/oe+8mjE1NjIyMjg5NDYxDQror7fliqHlv4XlsIrph43nn6Xor4bkuqfmnYMg5Lil5qC85L+d6K+B5LiN5oG25oSP5Lyg5pKt5Lqn5ZOB5rqQ56CB44CB5LiN5b6X55u05o6l5a+55Lqn5ZOB5pys6LqrDQrov5vooYzkuozmrKHovazllK7miJblgJLljZbjgIHkuI3lvpflr7nkuqflk4Hov5vooYznroDljZXljIXoo4XlkI7lo7Dnp7DkuLroh6rlt7HnmoTkuqflk4HnrYnjgIINCkNvcHlyaWdodCAoYykgMjAxOC0yMDE5IGh0dHA6Ly93d3cuc3V4aWFuZ3cuY29tLyBBbGwgcmlnaHRzIHJlc2VydmVkLg==');
		videoUrl=api.pageParam['videoUrl'];
		imgurl=api.pageParam['imgurl'];
		if(imgurl){
			$('.video_loading img').attr('src',imgurl);
		}
		video_content=api.pageParam['content'];
		video_money = api.pageParam['money'];
		fabu();
	}
	// 发布视频开始
	function fabu() {

		// _loading('1视频上传中...');
		if (api.systemType == 'ios') {
			return ios();
		} else {
			return android();
		}
	}
	// 转码
	function ios() {
		var videoTool = api.require('videoTool');
		jindu('正在转码');
		videoTool.captureScreen({
			path: videoUrl,
			startTime: 0
		}, function(ret, err) {
			if (ret.state == 0 && ret.imgPath) {
				var img = ret.imgPath;
				$('.video_loading img').attr('src',img);
				videoTool.compressVideo({
					directories: videoUrl,
					format: 'mp4',
					presetName: 2
				}, function(ret, err) {
					if (ret.state == 0 && ret.compressPath) {
						videoUrl = ret.compressPath;
						// 上传缩略图以及视频
						_qiniu(img, function(ret, err) {
							console.log('上传缩略图：' + JSON.stringify(ret) + JSON.stringify(err))
							if (ret.status) {
								console.log(JSON.stringify(ret));
								if (ret.oper == "complete" && ret.info.key) {
									imgurl = ret.info.key;
									//上传成功后组装访问路径，或直接访问文档
									_qiniu(videoUrl, function(ret, err) {
										console.log('上传视频：' + JSON.stringify(ret) + JSON.stringify(err))
										if (ret.status) {
											console.log(JSON.stringify(ret));
											if (ret.oper == "complete") {
												//上传成功后组装访问路径，或直接访问文档
												var url = ret.info.key;
												if (url) {
													// 发布视频
													_ajax('member/video_add', function(ret, err) {
														_loadingCloes();
														if (ret) {
															if (ret.ret) {
																if (ret.ret) {
																	_alert(ret.ret);
																} else {
																	_alert(ret.err);
																}
																_removePrefs('index_video_loading');
																return api.closeFrame();
															} else {
																_alert(ret.err);
															}
														} else {
															_alert('视频上传错误video_add：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
														}
													}, {
														url: url,
														city: _city(),
														content: video_content,
														img: imgurl,
														video_money:video_money,
													})
												} else {
													// _loadingCloes();
													_alert('视频上传url不存在');
													return api.closeFrame();
												}
											} else if (ret.oper == "progress") {
												jindu(Math.round(parseInt(ret.percent))+'%');
												//上传过程中获取进度数据
											}
										} else {
											// _loadingCloes();
											_alert('错误_qiniu：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
											console.log(JSON.stringify(err));
											return api.closeFrame();
										}
									})
								} else if (ret.oper == "progress") {
									jindu(Math.round(parseInt(ret.percent))+'%');
									//上传过程中获取进度数据
								}
							} else {
								// _loadingCloes();
								_alert('图片错误_qiniu：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
								console.log(JSON.stringify(err));
								return api.closeFrame();
							}
						})
					} else {
						_alert('视频转码失败：'+ret.state)
						return api.closeFrame();
					}
				});
			}else{

			}
		});
	}
	function android() {
		_qiniu(videoUrl, function(ret, err) {
			console.log('上传视频：' + JSON.stringify(ret) + JSON.stringify(err))
			if (ret.status) {
				if (ret.oper == "complete") {
					//上传成功后组装访问路径，或直接访问文档
					var url = ret.info.key;
					if (url) {
						var img_url = _sx_qiniu + url + '?vframe/jpg/offset/0/w/720/h/1280/format/png';
						api.imageCache({
							url: img_url,
							thumbnail: false
						}, function(ret, err) {
							console.log('缓存缩略图：' + JSON.stringify(ret) + JSON.stringify(err))
							if (ret) {
								if (ret.status) {
									_qiniu(ret.url, function(ret, err) {
										console.log('上传缩略图：' + JSON.stringify(ret) + JSON.stringify(err))
										if (ret.status) {
											console.log(JSON.stringify(ret));
											if (ret.oper == "complete" && ret.info.key) {
												imgurl = ret.info.key;
												//上传成功后组装访问路径，或直接访问文档
												// 发布视频
												_ajax('member/video_add', function(ret, err) {
													console.log('发布数据：' + JSON.stringify(ret) + JSON.stringify(err))
													// _loadingCloes();
													if (ret) {
														if (ret.ret) {
															_alert(ret.ret);
														} else {
															_alert(ret.err);
														}
														_removePrefs('index_video_loading');
														return api.closeFrame();
													} else {
														_alert('视频上传错误video_add：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
													}
												}, {
													url: url,
													city: _city(),
													content: video_content,
													img: imgurl,
													video_money:video_money,
												})
											} else if (ret.oper == "progress") {
												jindu(Math.round(parseInt(ret.percent))+'%');
												//上传过程中获取进度数据
											}
										} else {
											// _loadingCloes();
											_alert('图片错误_qiniu：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
											console.log(JSON.stringify(err));
											return api.closeFrame();
										}
									})
								} else {
									_alert('视频缩略图获取失败，请重新发布');
									return api.closeFrame();
								}
							} else {
								_alert('视频缩略图获取失败，请重新发布2');
								return api.closeFrame();
							}
						});
					} else {
						// _loadingCloes();
						_alert('视频上传url不存在');
						return api.closeFrame();
					}
				} else if (ret.oper == "progress") {
					jindu(Math.round(parseInt(ret.percent))+'%');
					//上传过程中获取进度数据
				}
			} else {
				// _loadingCloes();
				_alert('错误_qiniu：' + JSON.stringify(err) + '，请将错误提示截图交给客服处理')
				console.log(JSON.stringify(err));
				return api.closeFrame();
			}
		})
	}
	// 发布视频结束
	// 显示发布进度
	function jindu(a){
		$('#jindi').text(a);
	}
</script>

</html>
